using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Ledger.BalanceSplit;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Services.Ledger.BalanceSplit
{
    /// <summary>
    /// 余额分账服务类
    /// </summary>
    public class BalanceSplitService : BaseService
    {
        /// <summary>
        /// 创建余额分账服务类
        /// </summary>
        /// <param name="config">配置对象</param>
        public BalanceSplitService(Config config) : base(config)
        {
        }

        /// <summary>
        /// 获取API URL
        /// </summary>
        /// <param name="requestClass">请求类名</param>
        /// <param name="apiPath">接口路径</param>
        /// <returns>完整的API URL</returns>
        private string GetApiUrl(Type requestClass, string? apiPath = null)
        {
            if (!string.IsNullOrEmpty(apiPath))
            {
                return apiPath;
            }

            // 使用请求类中定义的ApiPrefix和ApiUri常量
            var apiPrefixField = requestClass.GetField("ApiPrefix");
            var apiUriField = requestClass.GetField("ApiUri");
            
            if (apiPrefixField != null && apiUriField != null)
            {
                string apiPrefix = (string)apiPrefixField.GetValue(null)!;
                string apiUri = (string)apiUriField.GetValue(null)!;
                
                // 组合API路径
                string path = apiPrefix.TrimStart('/') + "/" + apiUri.TrimStart('/');
                return "/" + path;
            }

            // 如果没有定义ApiPrefix和ApiUri，使用默认路径
            return "/open/ledger/balancesplit";
        }

        /// <summary>
        /// 余额查询
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BalanceQueryResponse> QueryBalanceAsync(BalanceQueryRequest request)
        {
            string apiPath = GetApiUrl(typeof(BalanceQueryRequest));
            return await PostAsync<BalanceQueryRequest, BalanceQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 余额分账
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BalanceSplitResponse> SplitBalanceAsync(BalanceSplitRequest request)
        {
            string apiPath = GetApiUrl(typeof(BalanceSplitRequest));
            return await PostAsync<BalanceSplitRequest, BalanceSplitResponse>(request, apiPath);
        }

        /// <summary>
        /// 余额分账查询
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BalanceSplitQueryResponse> QueryBalanceSplitAsync(BalanceSplitQueryRequest request)
        {
            string apiPath = GetApiUrl(typeof(BalanceSplitQueryRequest));
            return await PostAsync<BalanceSplitQueryRequest, BalanceSplitQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 余额退款
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BalanceRefundResponse> RefundBalanceAsync(BalanceRefundRequest request)
        {
            string apiPath = GetApiUrl(typeof(BalanceRefundRequest));
            return await PostAsync<BalanceRefundRequest, BalanceRefundResponse>(request, apiPath);
        }

        /// <summary>
        /// 余额退款查询
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BalanceRefundQueryResponse> QueryBalanceRefundAsync(BalanceRefundQueryRequest request)
        {
            string apiPath = GetApiUrl(typeof(BalanceRefundQueryRequest));
            return await PostAsync<BalanceRefundQueryRequest, BalanceRefundQueryResponse>(request, apiPath);
        }
    }
} 